/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package embeddedpaint.user_interface;

import embeddedpaint.model.layers.ALayer;
import embeddedpaint.model.layers.LayerManager;
import embeddedpaint.model.layers.LayerText;
import embeddedpaint.user_interface.tools.ToolsCreateTextLayer;
import embeddedpaint.user_interface.tools.ToolsManageLayer;
import embeddedpaint.user_interface.tools.ToolsTransformLayer;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import javax.swing.JSeparator;

public class PopupMenuForLayers extends JPopupMenu
{
    private static final String M_LABEL_ACTION_RENAME           = "Rename";
    private static final String M_LABEL_ACTION_DELETE           = "Delete";
    private static final String M_LABEL_ACTION_EDIT_TEXT        = "Edit Text";
    private static final String M_LABEL_ACTION_CONVERT_TO_IMAGE = "Convert to Image";
    private static final String M_LABEL_ACTION_MERGE            = "Merge Layers";
    
    private ALayer              m_layer;
    private LayerManager        m_layerManager;
    private Container           m_frame;
    private JMenuItem           m_menuRename;
    private JMenuItem           m_menuDelete;
    private JMenuItem           m_menuEditTextLayer;
    private JMenuItem           m_menuCreateImageLayer;
    private JMenuItem           m_menuMergeImageLayer;

    public PopupMenuForLayers (Container window, LayerManager layerManager)
    {
        this.m_frame        = window;
        this.m_layerManager = layerManager;

        this.init();
    }

    public void setLayer (ALayer layer)
    {
        this.m_layer = layer;
        
        activeMultiLayerAction();
        
        activeTextLayerAction();
    }

    @Override
    public void setVisible (boolean b)
    {
        super.setVisible(b);
    }

    private void init ()
    {
        this.m_menuRename           = new JMenuItem(M_LABEL_ACTION_RENAME);
        this.m_menuDelete           = new JMenuItem(M_LABEL_ACTION_DELETE);
        this.m_menuEditTextLayer    = new JMenuItem(M_LABEL_ACTION_EDIT_TEXT);
        this.m_menuCreateImageLayer = new JMenuItem(M_LABEL_ACTION_CONVERT_TO_IMAGE);
        this.m_menuMergeImageLayer  = new JMenuItem(M_LABEL_ACTION_MERGE);

        this.m_menuDelete.addActionListener(new ActionListener()
        {
            @Override
            public void actionPerformed (ActionEvent e)
            {
                ToolsManageLayer.deleteLayer(m_layerManager, m_frame, m_layer);
            }
        });

        this.m_menuRename.addActionListener(new ActionListener()
        {
            @Override
            public void actionPerformed(ActionEvent e)
            {
                ToolsManageLayer.renameLayer(m_layerManager, m_frame, m_layer);
            }
        });

        this.m_menuEditTextLayer.addActionListener(new ActionListener()
        {
            @Override
            public void actionPerformed(ActionEvent e)
            {
                ToolsCreateTextLayer.editTextLayer(m_frame, m_layerManager, (LayerText) m_layer);
            }
        });

        this.m_menuCreateImageLayer.addActionListener(new ActionListener()
        {
            @Override
            public void actionPerformed (ActionEvent e)
            {
                ToolsTransformLayer.convertlayerTextToLayerImage(m_layerManager, (LayerText) m_layer);
            }
        });

        this.m_menuMergeImageLayer.addActionListener(new ActionListener()
        {
            @Override
            public void actionPerformed (ActionEvent e)
            {
                ToolsManageLayer.mergeLayerImage(m_layerManager, m_frame);
            }
        });

        this.add(this.m_menuDelete);
        this.add(this.m_menuRename);
        this.add(this.m_menuMergeImageLayer);
        this.add(new JSeparator());
        this.add(this.m_menuEditTextLayer);
        this.add(this.m_menuCreateImageLayer);
    }

    private void activeMultiLayerAction ()
    {
        if (m_layerManager.getM_selectedImage().size() > ToolsManageLayer.M_MINIMAL_NUMBER_TO_MULTI_LAYER && m_layerManager.selectedLayerIsOnlyLayerImage())
        {
            this.m_menuMergeImageLayer.setEnabled(true);
        }
        else
        {
            this.m_menuMergeImageLayer.setEnabled(false);
        }
    }

    private void activeTextLayerAction ()
    {
        if (this.m_layer instanceof LayerText)
        {
            this.m_menuEditTextLayer   .setEnabled(true);
            this.m_menuCreateImageLayer.setEnabled(true);
        }
        else
        {
            this.m_menuEditTextLayer   .setEnabled(false);
            this.m_menuCreateImageLayer.setEnabled(false);
        }
    }
}
